import { to } from 'await-to-js';
import { signin } from '../db/user';

export default defineEventHandler(async event => {
    const { name, password } = await readBody(event);

    const [error, data] = await to(signin(name, password));

    if (error) return { code: 1, msg: 'Invalid name or password!', data: null };
    else return { code: 0, msg: `Welcome back, ${name}!`, data };
});